﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TTGame;

namespace TTGameEngine {
public class SkillPackage : MonoBehaviour {


    //按ID分类的技能<SkillID, skillObj>
    Dictionary<int, SkillTL> skillObjCaches = new Dictionary<int, SkillTL>();
    SkillTL activeSkill;  //大招技能
    SkillTL attackSkill;  //普攻技能
    //其他的技能
    List<SkillTL> normalSkillObjs = new List<SkillTL>();

    void Awake() {
        InitLocalSkills();
    }

    public void InitLocalSkills() {
        SkillTL[] allSkillObjs = gameObject.GetComponentsInChildren<SkillTL>(false);
        for(int i = 0; i < allSkillObjs.Length; i++) {
            SkillTL skill = allSkillObjs[i];
            skillObjCaches.Add(skill.SkillID, skill);
            switch(skill.m_skillData.m_skillType) {
                case SkillType.ActiveSkill:
                    activeSkill = skill;
                    break;
                case SkillType.Attack:
                    attackSkill = skill;
                    break;
                case SkillType.NormalSkill:
                    normalSkillObjs.Add(skill);
                    break;
            }
        }
    }

    public void InitWithServerData() {
        if(activeSkill != null) activeSkill.InitWithServerData();
        if(attackSkill != null) attackSkill.InitWithServerData();
        for(int i = 0; i < normalSkillObjs.Count; i++) {
            normalSkillObjs[i].InitWithServerData();
        }
    }

    public int GetAISkillCount() {
        return normalSkillObjs.Count;
    }

    /// <summary>
    /// 根据Idx创建skillID。
    ///
    /// 注意得到的Skill对象是设置为隐藏的。
    /// </summary>
    /// <param name="skillID"></param>
    /// <returns></returns>
    public SkillTL GetAISkillByIdx(int skillIdx) {
        if(skillIdx < 0 || skillIdx >= normalSkillObjs.Count) {
            DebugUtil.LogWarningFormat("[SkillTLData] 没有找到对应Idx[{0}]的技能。BindObj[{1}]", skillIdx, transform.parent.name);
            return null;
        }

        SkillTL skill = normalSkillObjs[skillIdx];

        return skill;
    }

    public SkillTL GetSkillByID(int skillID) {
        SkillTL skill = null;
        skillObjCaches.TryGetValue(skillID, out skill);
        if(skill == null) {
            DebugUtil.LogWarningFormat("[SkillTLData] 没有找到对应ID[{0}]的技能。BindObj[{1}]", skillID, transform.parent.name);
        }
        return skill;
    }

    public SkillTL GetAISkillPrepared() {
        SkillTL skill = null;
        for(int i = 0; i < normalSkillObjs.Count; i++) {
            SkillTL tmpSkill = GetAISkillByIdx(i);
            if(tmpSkill != null && tmpSkill.IsPrepared()) {
                skill = tmpSkill;
                break;
            }
        }
        return skill;
    }

    public SkillTL GetActiveSkill() {
        return activeSkill;
    }

    public SkillTL GetAttackSkill() {
        return attackSkill;
    }

    public SkillTL GetFirstNormalSkill() {
        if(normalSkillObjs.Count <= 0) {
            return null;
        }
        return normalSkillObjs[0];
    }

}
}